package controller

import (
	"fmt"
	"my_first_try/common"
	"my_first_try/model"
	"net/http"
	"strconv"

	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
)

//找到所有采购明细信息，并传回到网页上 （页面用）
func E_Set_All_Purchase_details(c *gin.Context) {
	DB := common.GetDB()
	var purchase_details []model.Purchase_detail //结构体数组
	// 显示所有的采购明细信息
	result := DB.Table("purchase_details").Find(&purchase_details)
	if result != nil {
		fmt.Println("!!")
	}
	fmt.Println(purchase_details)
	c.JSON(http.StatusOK, gin.H{ //多传一些参数 可能会用到
		"code":             "200",
		"purchase_details": purchase_details, //试试传一个列表
		"msg":              "成功",
	})
}

//查找某一指定采购明细信息(网页用)
func E_Get_Purchase_detail(c *gin.Context) {
	DB := common.GetDB()
	var test model.Purchase_detail
	var test2 []model.Purchase_detail
	request := make(map[string]interface{}) //新建一个map类型的变量request，保存前端传来的数据
	//获取参数
	fmt.Println(request)
	c.ShouldBind(&request) //将前端传过来的数据绑定到该request上
	fmt.Println(request)
	test.Purchase_details_id = request["id"].(string) //取得相应的map值 放到结构体上
	// 查询采购明细是否存在(链接数据库查)
	DB.Where("purchase_details_id = ?", test.Purchase_details_id).First(&test2)
	if test2[0].Purchase_details_id != "" {
		fmt.Println("成功查找到该采购明细")
		c.JSON(200, gin.H{
			"code":             "200",
			"purchase_details": test2, //传一个对象
			"msg":              "成功",
		})
	} else {
		c.JSON(200, gin.H{
			"code":             "404",
			"purchase_details": test2, //传一个空对象
			"msg":              "no",
		})
		fmt.Println("未找到该采购明细")
	}
}

//增加采购明细（页面用）
func E_Add_Purchase_detail(c *gin.Context) {
	DB := common.GetDB()
	request := make(map[string]interface{}) //新建一个map类型的变量request，保存前端传来的数据
	c.ShouldBind(&request)                  //将前端传过来的数据绑定到该request上
	var test model.Purchase_detail
	test.Purchase_details_id = request["purchase_details_id"].(string)             //获取前端的要增加的采购明细的属性
	test.Purchase_details_list = request["purchase_details_list"].(string)         //
	test.Purchase_details_goods_id = request["purchase_details_goods_id"].(string) //
	test.Purchase_details_count, _ = strconv.Atoi(request["purchase_details_count"].(string))
	test.Purchase_details_price, _ = strconv.Atoi(request["purchase_details_price"].(string))
	test.Purchase_details_prices, _ = strconv.Atoi(request["purchase_details_prices"].(string))
	test.Purchase_details_remarks = request["purchase_details_remarks"].(string) //
	fmt.Println(request)
	//获取到新增采购明细的所有信息
	if Insert_Purchase_detail(DB, test) {
		//没找到，可以新建该采购明细
		c.JSON(200, gin.H{
			"code": "200",
			"msg":  "添加成功",
		})
		fmt.Println("添加成功")
	} else {
		//能找到，就不可以新建该采购明细
		c.JSON(200, gin.H{
			"code": "404",
			"msg":  "添加失败",
		})
		fmt.Println("添加失败")
	}
}

//删除指定采购明细（页面用）
func E_Delete_Purchase_detail(c *gin.Context) {
	DB := common.GetDB()
	request := make(map[string]interface{}) //新建一个map类型的变量request，保存前端传来的数据
	c.ShouldBind(&request)                  //将前端传过来的数据绑定到该request上
	var test model.Purchase_detail
	test.Purchase_details_id = request["purchase_details_id"].(string) //获取前端的要删除的采购明细的id
	if Delete_Purchase_detail(DB, test) {
		//删除成功
		c.JSON(200, gin.H{
			"code": "200",
			"msg":  "删除成功",
		})
		fmt.Println("删除成功")
	} else {
		//删除失败
		c.JSON(200, gin.H{
			"code": "404",
			"msg":  "删除失败",
		})
		fmt.Println("删除失败")
	}
}

//修改指定采购明细信息（页面用）
func E_Modify_Purchase_detail(c *gin.Context) {
	DB := common.GetDB()
	request := make(map[string]interface{}) //新建一个map类型的变量request，保存前端传来的数据
	c.ShouldBind(&request)                  //将前端传过来的数据绑定到该request上
	var test model.Purchase_detail
	test.Purchase_details_id = request["purchase_details_id"].(string)             //获取前端的要增加的采购明细的属性
	test.Purchase_details_list = request["purchase_details_list"].(string)         //
	test.Purchase_details_goods_id = request["purchase_details_goods_id"].(string) //
	test.Purchase_details_count, _ = strconv.Atoi(request["purchase_details_count"].(string))
	test.Purchase_details_price, _ = strconv.Atoi(request["purchase_details_price"].(string))
	test.Purchase_details_prices, _ = strconv.Atoi(request["purchase_details_prices"].(string))
	test.Purchase_details_remarks = request["purchase_details_remarks"].(string) //
	//获取到全部修改信息

	if Modify_Purchase_detail(DB, test) {
		c.JSON(200, gin.H{
			"code": "200",
			"msg":  "删除成功",
		})
	} else {
		c.JSON(200, gin.H{
			"code": "404",
			"msg":  "删除失败",
		})
	}
}

//查询采购明细是否存在（新建采购明细用）
func Find_Purchase_detail_2(db *gorm.DB, test model.Purchase_detail) bool {
	var Purchase_detail model.Purchase_detail
	db.Where("Purchase_details_id = ?", test.Purchase_details_id).First(&Purchase_detail)
	if Purchase_detail.Purchase_details_id != "" {
		fmt.Println(Purchase_detail.Purchase_details_id)
		return true
	}
	return false
}

// 插入采购明细
func Insert_Purchase_detail(db *gorm.DB, test model.Purchase_detail) bool {
	if Find_Purchase_detail_2(db, test) {
		fmt.Println("该采购明细已经存在了！不能插入!")
		return false
	} else {
		db.Create(&test)
		fmt.Println("插入成功")
		return true
	}
}

// 删除采购明细
func Delete_Purchase_detail(db *gorm.DB, test model.Purchase_detail) bool {
	if Find_Purchase_detail_2(db, test) {
		fmt.Println("已经查找到该采购明细")
		db.Where("Purchase_details_id = ?", test.Purchase_details_id).Delete(&test)
		return true
	} else {
		fmt.Println("并不存在该采购明细")
	}
	return false
}

// 修改采购明细
func Modify_Purchase_detail(db *gorm.DB, test model.Purchase_detail) bool {
	if Find_Purchase_detail_2(db, test) {
		fmt.Println("已经查找到该采购明细")
		db.Model(&test).Where("Purchase_details_id = ?", test.Purchase_details_id).Updates(
			model.Purchase_detail{
				Purchase_details_id:       test.Purchase_details_id,
				Purchase_details_list:     test.Purchase_details_list,
				Purchase_details_goods_id: test.Purchase_details_goods_id,
				Purchase_details_count:    test.Purchase_details_count,
				Purchase_details_price:    test.Purchase_details_price,
				Purchase_details_prices:   test.Purchase_details_prices,
				Purchase_details_remarks:  test.Purchase_details_remarks,
			},
		)
		fmt.Println("修改完成")
		return true
	} else {
		fmt.Println("并不存在该采购明细")
	}
	return false
}
